﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Unity;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.UI;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCP.UI.PopWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCP.UI.Blocks;
using System.Windows.Browser;

namespace Baosight.ZNH.ZMCP.UI
{
    /// <summary>
    /// 采煤不定期验收
    /// </summary>
    /// Title:ZMCP20
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Version: 1.0
    /// History:
    ///       2012-04-11  张志芳 [新增]
    ///  
    public partial class ZMCP20 : EFViewCmes
    {
        public ZMCP20()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }


        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }
        /// <summary>
        /// 新增验收明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "采煤不定期验收—新增", "insert", null, sender, e);
        }
        /// <summary>
        /// 修改验收明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> rows = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            EIRow row = new EIRow();

            if (rows.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), rows[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            base.OnUpdateButtonClickPopup(eiInfo, "采煤不定期验收-修改", "update", null, sender, e);
        }
        /// <summary>
        /// 删除验收明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnDeleteButtonClick(sender, e);
        }

        /// <summary>
        /// 发布
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnPublishButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnModifyButtonClick("publish", "发布", sender, e);
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnPrintButtonClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            List<EIRow> EFGridCheckedRows = this.grid.CheckedEIRows;
            int count = EFGridCheckedRows.Count;
            if (count != 1)
            {
                BaseTools.OpenEFWindowAlert("请选择一条记录进行打印！", WindowAlertClosed);
                return;
            }
            EIInfo info = new EIInfo();
            info.Set("date", EFGridCheckedRows[0]["fdDate"]);
            info.Set(EIConstants.ServiceNameKey, "ZMCP20");
            info.Set(EIConstants.MethodNameKey, "printQuery");
            base.Submit(info, printQueryCompleted);            
        }

        /// <summary>
        /// 打印查询回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void printQueryCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                this.OpenEFWindowAlert("打印前查询失败！", true);
                this.TempButtonOpen(true);
                return;
            }
            Uri uri = Uploader.ConstructAbsoluteUri("../admin/RunmaxReport.jsp?projectName=CMESReport&reportName=ReportInfo3&mineDate=" + e.EIInfo.Get("date")+"&mineMonthBeginDate=" + e.EIInfo.Get("monthBeginDate")+"&mineYearBeginDate=" + e.EIInfo.Get("yearBeginDate"));
            HtmlPage.Window.Navigate(uri, "_blank");
            this.TempButtonOpen(true);
        }

        /// <summary>
        /// 查看明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "采煤不定期验收-查看", "detail", null, sender, e);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cp.engineering"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cp.department"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cp.workNum"));

            AddMineValidateData view = new AddMineValidateData(_dialogType);//构造一个新的修改页面
            view.EIDataContext = new Platform.Core.UI.EIDataContext();
            view.EIDataContext.Update(_eIInfo);
            view.measuringMethod = this.EIDataContext.GetAttribute("measuringMethod").ToString();
            view.isCalculateEnd = this.EIDataContext.GetAttribute("isCalculateEnd").ToString();
            view.mineValidateFormula = this.EIDataContext.GetAttribute("mineValidateFormula").ToString();
            view.coalDensity = this.EIDataContext.GetAttribute("coalDensity").ToString();
            view.gangueDensity = this.EIDataContext.GetAttribute("gangueDensity").ToString();

            if ("0".Equals(this.EIDataContext.GetAttribute("measuringMethod")))
            {
                view.downGrid.Visibility = Visibility.Collapsed;
                view.fdDownLength.eRegex = "";
                view.upLabel.Visibility = Visibility.Collapsed;
            }

            if ("insert".Equals(_dialogType))
            {
                view.fdType.Value = "不定期验收";
                view.fdStatus.Value = "草拟";
                view.fdPeople.Value = this.EIDataContext.GetAttribute("userName");
            }

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = view;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 在删除和上报时进行校验，已上报的不能删除和上报
        /// </summary>
        /// <param name="_eIRows"></param>
        /// <returns></returns>
        public override bool BeforeMethod(List<EIRow> _eIRows)
        {
            foreach (EIRow row in _eIRows)
            {
                if (row["fdStatus"].Equals("生效"))
                {
                    BaseTools.OpenEFWindowAlert("勾选的记录包含已发布记录，不能" + TempButton.Content + "，请重新选择!", null);
                    this.TempButtonOpen(true);
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 切换页签
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TabSource_SelectionChanged(object sender, RoutedEventArgs e)
        {
            EFTab _tab = sender as EFTab;
            if (null != _tab)
            {
                EFTabItem _item = _tab.SelectedItem as EFTabItem;

                if (null != _item && this.month.IsSelected && this.month.Content == null)
                {
                    this.month.LoadView<EFTabItem>("ZMCP21");
                }
            }
        }
    }
}
